<script setup>
import { ref, defineProps, defineEmits } from 'vue'
import { Lock, UserFilled } from '@element-plus/icons-vue'

// 定义props和emit
const props = defineProps({
  loginForm: {
    type: Object,
    default: () => ({
      phone: '',
      password: '',
    }),
  },
})
const emit = defineEmits(['signIn'])

// 表单验证
const validatePhone = (rule, value, callback) => {
  const phoneRegex = /^1[3-9]\d{9}$/

  if (!phoneRegex.test(value)) {
    callback(new Error('请输入有效的手机号'))
  } else {
    callback()
  }
}

const validatePassword = (rule, value, callback) => {
  if (value.length < 6) {
    callback(new Error('密码不能少于6位'))
  } else {
    callback()
  }
}

const loginRef = ref(null)
const loginForm = ref(props.loginForm)
const loginRules = ref({
  phone: [
    { required: true, message: '请输入电话', trigger: 'blur' },
    {
      validator: validatePhone,
      trigger: 'blur',
    },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    {
      validator: validatePassword,
      trigger: 'blur',
    },
  ],
})
const handleSignIn = () => {
  loginRef.value.validate((validate) => {
    if (validate) {
      emit('signIn', loginForm.value)
    }
  })
}
</script>

<template>
  <div class="sign-in">
    <!-- Logo + 标题 -->
    <div class="header">
      <img class="logo" src="/public/images/logo.png" alt="" />
      <h1 class="title">登录</h1>
    </div>
    <div class="content">
      <!-- 表单 -->
      <!-- 账号（邮箱或电话） -->
      <el-form :model="loginForm" :rules="loginRules" ref="loginRef" class="login-form">
        <el-form-item class="form-item" prop="phone">
          <label>电话号码</label>
          <el-input
            v-model="loginForm.phone"
            placeholder="输入电话"
            class="custom-input"
            :prefix-icon="UserFilled"
          />
        </el-form-item>
        <!-- 密码 -->
        <el-form-item class="form-item" prop="password">
          <div class="password-label">
            <label>密码</label>
            <!-- 忘记密码组件切换 -->
            <p
              class="forgot-password"
              @click="$router.push({ path: '/login/signup', query: { id: 'forgetPassword' } })"
            >
              忘记密码?
            </p>
          </div>
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="输入密码"
            class="custom-input"
            :show-password="true"
            :prefix-icon="Lock"
          />
        </el-form-item>

        <el-button type="primary" class="signin-btn" @click="handleSignIn()">登 录</el-button>

        <!-- 底部组件切换 -->
        <div class="divider">
          <span class="divider-text">或者使用其他方式</span>
        </div>
      </el-form>
    </div>

    <div class="footer">
      <span class="register-text">
        还没有账号？
        <p
          class="register-link"
          @click="$router.push({ path: '/login/signup', query: { id: 'register' } })"
        >
          注册一个
        </p>
      </span>
    </div>
  </div>
</template>

<style scoped lang="scss">
.sign-in {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;

  /* 让 logo 靠左，标题居中 */
  .header {
    position: relative;
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: space-between;
    .logo {
      width: 100px;
    }

    .title {
      position: absolute;
      left: 50%;
      font-size: 24px;
      font-weight: bold;
      transform: translate(-50%);
    }
  }

  .content {
    width: 100%;
    padding: 10px;
    .login-form {
      .form-item {
        margin-bottom: 24px;

        label {
          display: block;
          margin-bottom: 8px;
          color: #000000;
          font-size: 16px;
        }

        .password-label {
          width: 100%;
          display: flex;
          justify-content: space-between;
          align-items: center;
        }

        .forgot-password {
          font-size: small;
          color: #409eff;
          text-decoration: none;

          &:hover {
            text-decoration: underline;
            cursor: pointer;
          }
        }

        .custom-input {
          :deep(.el-input__wrapper) {
            padding: 10px;
            border-radius: 6px;
            box-shadow: 0 0 0 1px #dcdfe6;
            font-size: medium;
            &:focus-within {
              box-shadow: 0 0 0 1px #409eff;
            }
          }
        }
      }

      .signin-btn {
        width: 100%;
        height: 40px;
        margin-top: 10px;
        border-radius: 6px;
        font-size: medium;
      }

      .divider {
        margin: 24px 0;
        display: flex;
        align-items: center;
        color: #a8a8a8;

        &::before,
        &::after {
          content: '';
          flex: 1;
          border-bottom: 1px solid #dcdfe6;
        }

        .divider-text {
          padding: 0 12px;
          font-size: 12px;
        }
      }
    }
  }

  .footer {
    margin-top: 24px;
    text-align: center;

    .register-text {
      display: flex;
      color: #5a5a5a;
      font-size: 14px;

      .register-link {
        color: #409eff;
        text-decoration: none;

        &:hover {
          text-decoration: underline;
          cursor: pointer;
        }
      }
    }
  }
}
</style>
